<?php
	App::import("Model","BaseConfig");
	class PaymentHandler extends BaseConfig
	{
		var $name = "PaymentHandler";
		const ABSTRACT_PAYMENT_FILE = "abstract_payment_handler.php";
		var $cacheTime = '+20 minutes';
		function save(AbstractPaymentHandler $PaymentHandler,$data = null, $validate = true, $fieldList = array())
		{
			$this->setPaymentHandler($PaymentHandler);
			$this->set($data);
			if($this->validates()!== true)
			{
				return false;
			}
			$configData = cr($this->alias);
			$configData[$PaymentHandler->name] = $data[$this->alias];
			$data = $configData;
			foreach($data as $class => $config)
			{
				if(!$config)
				{
					$config = array();
				}
				$data[$class] = serialize($config);
			}
			$this->set(array());
			$data[$this->alias] = $data;
			return parent::save($data,false,$fieldList);
		}
		
		function setPaymentHandler(AbstractPaymentHandler $PaymentHandler)
		{
			$fieldproperies = $PaymentHandler->getFieldProperties();
			$this->fieldProperties = $fieldproperies;
			$this->validate = array();
			$this->addFieldPropertiesValidationRule();
		}
		
		function getPaymentHandlers()
		{
			$this->init();
			uses("folder");
            if(isset($this->paymentHandlers) && is_array($this->paymentHandlers))
            {
              return $this->paymentHandlers;
            }
			$path = $this->getPaymentHandlersPath();
			$Folder = new Folder($path);
			$files = $Folder->find('.*\.php');
			$handlers = array();
			if(!class_exists('AbstractPaymentHandler'))
			{
				require_once  $path.self::ABSTRACT_PAYMENT_FILE;
			}
			foreach($files as $file)
			{
				if(self::ABSTRACT_PAYMENT_FILE == $file)
				{
					continue;
				}
				require_once $path.$file;
				$file = str_replace('.php', "", $file);
				$file  = Inflector::camelize($file);
				$HandlerObject = $this->getPaymentHandler($file);
				if(is_object($HandlerObject))
				{
					$handlers[$file] = $HandlerObject;
				}
			}
            $this->paymentHandlers = $handlers;
			return $handlers;
		}
		function getPaymentHandlerObject($HandlerName)
		{
			$handlers = $this->getPaymentHandlers();
			$Handler = false;
			if(isset($handlers[$HandlerName]))
			{
				$Handler = $handlers[$HandlerName];
			}
			return $Handler;
		}
		private function getPaymentHandler($HandlerClass)
		{
			$name = $HandlerClass;
			$HandlerClass = $HandlerClass.'PaymentHandler';
			$PaymentHandler = false;
			$path = $this->getPaymentHandlersPath();
			if(is_subclass_of($HandlerClass, 'AbstractPaymentHandler'))
			{
				$PaymentHandler = new $HandlerClass;
				if(!isset($PaymentHandler->name) || !$PaymentHandler->name){
					$PaymentHandler->name = $name;
				}
				$PaymentHandler->setFieldPropetiesValue(cr($this->alias.'.'.$PaymentHandler->name));
			}
			return $PaymentHandler;
		}
		
		private function getPaymentHandlersPath()
		{
			$path = dirname(dirname(__FILE__));
			$path = $path.DS.'payment_handlers'.DS;
			return $path;
		}
		
		function init()
		{
			$config = cr($this->alias);
			if(is_null($config) || $config === false)
            {
              parent::init();
              $config = cr($this->alias);
              if(is_null($config) || $config === false)
              {
                $config = array();
              }
            }
            if($config && (!isset($this->configWritten) || $this->configWritten == false))
			{
				$newConfig = $config;
				foreach($config as $class => $properties)
				{
					if(is_string($properties))
					{
						$val = @unserialize($properties);
						if(!is_null($val))	
							$newConfig[$class] = $val;
						else
							$newConfig[$class] = $properties;
					}
				}
                $this->configWritten = true;
                Configure::write($this->alias,$newConfig);
			}
		}
	}
?>